//
//  BFAddMealViewController.m
//  BFoodSystem
//
//  Created by 浙江择富 on 2018/2/3.
//  Copyright © 2018年 陈名正. All rights reserved.
//

#import "BFAddMealViewController.h"
#import "BFFoodDetailTopCell.h"
#import "BFMealTextTableViewCell.h"
#import "BFFoodServices.h"
#import "BFFoodClassModel.h"
#import "BFFoodTypeModel.h"
#import "BFMealListTableViewCell.h"
#import "BFMealFooterView.h"
#import "BFAddMealDetailViewController.h"

@interface BFAddMealViewController ()<UITableViewDelegate,UITableViewDataSource,AddMealcellDelegate>
@property (nonatomic,strong)UITableView *tabView;
@property (nonatomic, strong) NSMutableArray *foodClassArr;
@end

@implementation BFAddMealViewController
- (UITableView *)tabView{
    if (!_tabView) {
        _tabView = [[UITableView alloc] init];
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.backgroundColor = [UIColor colorWithHex:BF_COLOR_B1];
        [_tabView registerNib:[UINib nibWithNibName:@"BFFoodDetailTopCell" bundle:nil] forCellReuseIdentifier:BFFoodDetailTopCellIdentifier];
        [_tabView registerNib:[UINib nibWithNibName:@"BFMealTextTableViewCell" bundle:nil] forCellReuseIdentifier:BFMealTextIdentifier];
  
         [_tabView registerNib:[UINib nibWithNibName:@"BFMealListTableViewCell" bundle:nil] forCellReuseIdentifier:BFMesalListIderfier];
        
        _tabView.tableFooterView = [[UIView alloc] init];
        _tabView.separatorColor = [UIColor clearColor];
        [_tabView registerClass:[BFMealFooterView class] forHeaderFooterViewReuseIdentifier:BFFooterIdentifier];
        
    }
    return _tabView;
}
- (NSMutableArray *)foodClassArr{
    if (!_foodClassArr) {
        _foodClassArr = [NSMutableArray array];
    }
    return _foodClassArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新增套餐";
    [self.view addSubview:self.tabView];
    [self setupBottomView];
    [self getFoodClassRequest];
   
}

- (void)setupBottomView{
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchUpInside];
    [submitBtn setBackgroundColor:[UIColor colorWithHex:BF_COLOR_B10]];
    submitBtn.titleLabel.font = [UIFont systemFontOfSize:BF_FONTSIZE_a3];
    submitBtn.layer.cornerRadius = 5;
    submitBtn.clipsToBounds = YES;
    [self.view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tabView);
        make.right.equalTo(self.tabView);
        make.bottom.equalTo(self.view).offset (-HOME_INDICATOR_HEIGHT);
        make.height.mas_equalTo(40);
    }];
    
    [self.tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(NavBarHeight);
        make.right.left.equalTo(self.view);
        make.bottom.equalTo(submitBtn.mas_top).offset(-HOME_INDICATOR_HEIGHT);
    }];
    
    
}
- (void)submitAction{
    
}

//**获取菜品列表数据*/
- (void)getFoodClassRequest{
    
    [[BFFoodServices alloc] getDishesListWtihSaleStatus:@"" SuccessBlock:^(id result) {
        self.foodClassArr = (NSMutableArray *)result;
        if (self.foodClassArr.count==0) {
            [BFUtils showAlertController:0 title:@"" message:@"暂无菜品分类信息，请新增菜品分类。"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
            return ;
        }
        for (BFFoodClassModel *classModel in self.foodClassArr) {
            classModel.isSelect = NO;
            for (BFFoodTypeModel *typeMdoel in self.foodModel.typeArr) {
                if ([typeMdoel.foodClassId integerValue] == [classModel.foodClassId integerValue] && [typeMdoel.is_shelf integerValue] == 1) {
                    classModel.isSelect = YES;
                }
            }
        }
        [self.tabView reloadData];
        
        
    } errorCode:^(NSInteger errorCode, NSString *errorMessage) {
        [BFUtils showAlertController:0 title:@"" message:errorMessage];
        
    } Failure:^(NSError *error) {
        [BFUtils showAlertController:0 title:@"" message:@"网络错误"];
        
    }];
    
}

#pragma -----tab的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *tabCell;
    
    if (indexPath.row == 0) {
        BFFoodDetailTopCell *cell = [tableView dequeueReusableCellWithIdentifier:BFFoodDetailTopCellIdentifier forIndexPath:indexPath];
        [cell updataCollectionViewWithFoodClassArr:self.foodClassArr];

        tabCell = cell;
        
    }else if (indexPath.row == 1){
       BFMealTextTableViewCell *textCell = [tableView dequeueReusableCellWithIdentifier:BFMealTextIdentifier forIndexPath:indexPath];
        if (self.foodModel) {
            [textCell setTextViewText:self.foodModel.foodDescription];
        }else{
            [textCell textViewPlacehodel];
        }
        
        tabCell = textCell;
    }else{
        BFMealListTableViewCell *listCell = [tableView dequeueReusableCellWithIdentifier:BFMesalListIderfier forIndexPath:indexPath];
        tabCell = listCell;
        
       
    }
    
    return tabCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   if (indexPath.row == 0) {
    return BFFoodDetailTopCellHeight;
  }else if(indexPath.row == 1){
    return 100;
   }else{
    return 80;
   }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==2) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView;
    
   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
    if (indexPath.row == 2) {
        BFMealFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:BFFooterIdentifier];
        footerView.delegate = self;
        footView = footerView;
        
    }
    return footView;
}

- (void)clickButton:(UIButton *)button{
        BFAddMealDetailViewController *addMealVC = [[BFAddMealDetailViewController alloc] init];
    [self.navigationController pushViewController:addMealVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
    if (indexPath.row == 2) {
        return 60;
    }
    return 0;
}
#pragma 侧滑删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // 删除数据源的数据,self.cellData是你自己的数据
//        [self.cellData removeObjectAtIndex:indexPath.row];
        // 删除列表中数据
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";//默认文字为 Delete
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
